package ltd.yangyh.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.cj.jdbc.Driver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.sql.SQLException;

@ComponentScan(basePackages = "ltd.yangyh")
@Configuration
public class SpringConfig {
    @Resource
    private DruidDataSource druidDataSource;

    @Bean
    public DruidDataSource druidDataSource() {

        DruidDataSource druidDataSource = new DruidDataSource();
        Driver driver = null;
        try {
            driver = new Driver();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/security2?serverTimezone=UTC");
        druidDataSource.setDriver(driver);
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        return druidDataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource);
        return jdbcTemplate;
    }
}
